package com.ruoyi.course.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.course.domain.Course;
import com.ruoyi.course.service.ICourseService;
import org.apache.commons.math3.stat.descriptive.moment.Skewness;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.course.domain.CourseWaitSelection;
import com.ruoyi.course.service.ICourseWaitSelectionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 待选课程管理Controller
 *
 * @author ruoyi
 * @date 2023-10-19
 */
@RestController
@RequestMapping("/course/course_wait_selection")
public class CourseWaitSelectionController extends BaseController {
    @Autowired
    private ICourseWaitSelectionService courseWaitSelectionService;

    @Autowired
    private ICourseService courseService;


    /**
     * 导入待选课程管理列表
     */
//    @PreAuthorize("@ss.hasPermi('course:course_wait_selection:import')")
    @Anonymous
    @GetMapping("/import")
    public AjaxResult importCourse() {
        CourseWaitSelection courseWaitSelection = new CourseWaitSelection();
        Course course = new Course();
        course.setCourseStatus("选课中");

        List<Course> courseList = courseService.selectCourseList(course);


        if (courseList.isEmpty()) {
            return error("可选课程为空！");
        }
//        System.out.println("-------------------------------------------------");
//        System.out.println("-------------------------------------------------");
        for (Course course1 : courseList) {
            courseWaitSelection.setCourseId(course1.getCourseId());
//            System.out.println(courseWaitSelection);
            if (courseWaitSelectionService.selectCourseWaitSelectionList(courseWaitSelection).isEmpty()) {
//                System.out.println(courseWaitSelectionService.selectCourseWaitSelectionList(courseWaitSelection));
//                System.out.println(courseWaitSelectionService.selectCourseWaitSelectionList(courseWaitSelection).isEmpty());
                BeanUtils.copyProperties(course1,courseWaitSelection);
                courseWaitSelection.setNeedNum(20L);
                courseWaitSelection.setSelectedNum(0L);
                courseWaitSelectionService.insertCourseWaitSelection(courseWaitSelection);
            }else {
                continue;
            }
        }
        return success();
    }


    /**
     * 查询待选课程管理列表
     */
//    @PreAuthorize("@ss.hasPermi('course:course_wait_selection:list')")
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(CourseWaitSelection courseWaitSelection) {
        System.out.println("111111111111111111111111111111111");
        startPage();
        List<CourseWaitSelection> list = courseWaitSelectionService.selectCourseWaitSelectionList(courseWaitSelection);
//        System.out.println(list);
        return getDataTable(list);
    }

    @Anonymous
    @GetMapping("/cwslist")
    public List cwslist(CourseWaitSelection courseWaitSelection) {

        List<CourseWaitSelection> list = courseWaitSelectionService.selectCourseWaitSelectionList(courseWaitSelection);
//        System.out.println(list);
        return list;
    }


    /**
     * 获取待选课程管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:course_wait_selection:query')")
    @GetMapping(value = "/{selectId}")
    public AjaxResult getInfo(@PathVariable("selectId") Long selectId) {
        return success(courseWaitSelectionService.selectCourseWaitSelectionBySelectId(selectId));
    }

    /**
     * 新增待选课程管理
     */
    @PreAuthorize("@ss.hasPermi('course:course_wait_selection:add')")
    @Log(title = "待选课程管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CourseWaitSelection courseWaitSelection) {
        return toAjax(courseWaitSelectionService.insertCourseWaitSelection(courseWaitSelection));
    }

    /**
     * 修改待选课程管理
     */
    @PreAuthorize("@ss.hasPermi('course:course_wait_selection:edit')")
    @Log(title = "待选课程管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CourseWaitSelection courseWaitSelection) {
        return toAjax(courseWaitSelectionService.updateCourseWaitSelection(courseWaitSelection));
    }

    /**
     * 删除待选课程管理
     */
    @PreAuthorize("@ss.hasPermi('course:course_wait_selection:remove')")
    @Log(title = "待选课程管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{selectIds}")
    public AjaxResult remove(@PathVariable Long[] selectIds) {
        return toAjax(courseWaitSelectionService.deleteCourseWaitSelectionBySelectIds(selectIds));
    }
}
